/**
 * 
 */
package net.oauth.core.authorization;

import net.oauth.core.credentials.oauth2.OAuth2ClientCredentials;
import net.oauth.exception.OAuthAuthorizationException;
import net.oauth.transport.http.HttpRequest;
import za.co.sindi.codec.Base64Codec;
import za.co.sindi.codec.exception.EncodingException;

/**
 * @author Buhake Sindi
 * @since 11 March 2012
 *
 */
public class OAuth2BasicAuthorization extends OAuthHttpAuthorization {

	private final OAuth2ClientCredentials clientCredentials;
	
	/**
	 * @param clientCredentials
	 */
	public OAuth2BasicAuthorization(OAuth2ClientCredentials clientCredentials) {
		super();
		if (clientCredentials == null) {
			throw new IllegalArgumentException("Client credentials may not be null.");
		}
		
		this.clientCredentials = clientCredentials;
	}

	/* (non-Javadoc)
	 * @see net.oauth.core.auth.OAuthAuthorizationScheme#getSchemeName()
	 */
	@Override
	public String getSchemeName() {
		// TODO Auto-generated method stub
		return "Basic";
	}

	/* (non-Javadoc)
	 * @see net.oauth.core.auth.OAuthHttpAuthorizationScheme#createAuthorizationString(net.oauth.core.OAuthClientCredentials, net.oauth.transport.http.HttpRequest)
	 */
	@Override
	protected String createAuthorizationString(HttpRequest request) throws OAuthAuthorizationException {
		// TODO Auto-generated method stub
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(clientCredentials.getClientId()).append(":").append(clientCredentials.getSecret() == null ? "" : clientCredentials.getSecret());
			return Base64Codec.encode(sb.toString(), "UTF-8");
		} catch (EncodingException e) {
			// TODO Auto-generated catch block
			throw new OAuthAuthorizationException(e.getLocalizedMessage(), e);
		}
	}
}
